import Common from '../common'
var Js = Common('notice')
export default {
	name: 'notice',
	mixins: [Js],
	components: {},
	data() {
		return {
			fields: [{
					key: 'id',
					label: 'ID',
					application: [],
					componentName: 'sls-input',
					listType: '',
					width: 50
				},
				{
					key: 'title',
					label: '标题',
					application: ['编辑', '添加'],
					componentName: 'sls-input',
					listType: 'normal',
				},
				{
					key: 'content',
					label: '公告内容',
					application: ['编辑', '添加'],
					componentName: 'tinymce-editor',
					listType: '',
					dialogStyle: 'width:100%;',
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};
						},
					},
					width: 200,
				},
				{
					label: '操作',
					listType: 'deal',
					width: 120
				},
			],

			// 按钮配置
			btn_info: [{
					type: '',
					icon: 'el-icon-edit-outline',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑'
					},
					func: {
						apiName: function(self) {
							return "api_articleUpdate"
						},
						formData: function(self) {
							return self.formData
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
								},
								data: self.submitData
							};
							if (self.submitData.parentid && self.submitData.parentid == self.formData.id) {
								self.$$notify('父级ID和子级ID重叠', 'fail');
								return false;
							} else {
								return postData;
							};
						}
					},
				},
				{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {
						apiName: function(self) {
							return "api_articleUpdate"
						},
						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray],
								},
								data: {
									status: -1
								}
							};
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					text: function(data) {
						return '添加'
					},
					func: {
						apiName: function(self) {
							return "api_articleAdd"
						},
						formData: function(self) {
							var data = {
								title: '',
								description: '',
								mainImg: [],
								bannerImg: [],
							};
							return data
						},
						postData: function(self) {
							self.submitData.menu_id = 3;
							var postData = {
								data: self.submitData
							};
							return postData;
						}
					},
				},

			],
			searchItem: {
				menu_id: 3
			},
			startSearchItem: {
				menu_id: 3
			},
			optionData: {
				labelOptions: [],
				statusOptions: [{
					text: '启用',
					value: 1
				}, {
					text: '禁用',
					value: -1
				}],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			}

		}

	},

	mounted() {
		const self = this;
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
      this.initMainData();

		},


		/**
		 * 列表主函数
		 */
		async initMainData(isNew) {

			const self = this;
			self.table_arguments.loading = true;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			var res = await self.$$api_articleGet({
				data: postData
			});
			self.mainData = res.info.data;
			self.contentData = self.mainData[0];
			self.paginate.count = res.info.total;
			self.table_arguments.loading = false;

		},

    async initMenuData() {
      const self = this;
      const postData = {};
      postData.searchItem = {
        status: 1,
      };
      postData.token = self.$store.getters.getToken;
      postData.order = {
        listorder: 'desc'
      };

      try {
        var res = await self.$$api_labelGet({
          data: postData
        });
      } catch (err) {
        console.log(err);
        self.$notify('网络故障', 'error');
      };

      if (res) {
        self.optionData.labelOptions = res.info.data;
        console.log('self.optionData.labelOptions', self.optionData.labelOptions)
        var idArray = []
        for(var i = 0;i < self.optionData.labelOptions.length;i++){
          idArray.push(self.optionData.labelOptions[i].id)
        }
        console.log('idArray',idArray)

        self.searchItem.menu_id = ['in',idArray];
        // self.searchItem.menu_id = self.optionData.labelOptions[0].id;
        // self.fields[5]['header_search']['defaultValue'] = self.searchItem.menu_id;
        // self.$set(self.fields,5,self.fields[5]);
        self.initMainData();
      };


    },



	},


}
